package panel.downloadZipPanel
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.caches.SaveZipAsFile;
	import assists.log.Log;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import main.ClientConstants;
	import main.LocalStoreInfo;
	
	
	public class DownloadResourceCityBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var back:Bitmap;
		private var backContainer:Sprite;
		
		private var cityText:TextField;
		
		private var downloadBtn:ButtonUtils;
		private var detailBtn:ButtonUtils;
		
		private var line:ProgressLine;
		
		private var _onShowDetail:Function;
		
		private var loadZip:SaveZipAsFile;
		
		private var city:Object;
		
		private var store:LocalStoreInfo;
		private var cityIsDone:Boolean = false;
		
		private var chooseThisCity:Function;
		
		public function DownloadResourceCityBox(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			store = new LocalStoreInfo;

			initThis();
		}
		
		private function initThis():void
		{
			backContainer = new Sprite;
			this.addChild(backContainer);
			back = new _loader.uiManager.componentBack as Bitmap;
			back.width = _stage.stageWidth;
			back.height = _stage.stageHeight * .1;
			backContainer.addChild(back);
			
			cityText = new TextField;
			cityText.mouseEnabled = true;
			cityText.selectable = false;
			cityText.multiline = false;
			cityText.wordWrap = false;
			cityText.x = _stage.stageWidth * .25;
			cityText.y = backContainer.height * .3;
			this.addChild(cityText);
			
			var img:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.downloadIcon,true);//new _loader.uiManager.btnBack as Bitmap;
			img.smoothing = true;
			img.height = _stage.stageHeight * .08;
			img.width = _stage.stageHeight * .08;
			downloadBtn = new ButtonUtils(img,downloadThis);
			downloadBtn.x = _stage.stageWidth * .05;
			downloadBtn.y = _stage.stageHeight * .01;
			this.addChild(downloadBtn);
			
			var img1:Bitmap = new _loader.uiManager.arrowIcon as Bitmap;
			img1.smoothing = true;
			img1.width = _stage.stageWidth * .1;
			img1.height = _stage.stageWidth * .1;
			detailBtn = new ButtonUtils(img1,showDetail);
			detailBtn.x = _stage.stageWidth * .85;
			detailBtn.y = _stage.stageHeight * .02;
			this.addChild(detailBtn);
			
			line = new ProgressLine(_stage,_loader,_lang,_uri,_stage.stageWidth,_stage.stageHeight * .01);
			line.x = 0;
			line.y = _stage.stageHeight * .09;
			this.addChild(line);
			
			backContainer.addEventListener(MouseEvent.CLICK,chooseThisHandler);
		}
		
		protected function chooseThisHandler(event:MouseEvent):void
		{
			if(chooseThisCity is Function){
				chooseThisCity(city.id);
			}
		}
		
		public function changeData(obj:Object):void
		{
			city = obj;
			
			TextFieldUtils.setHtmlText(cityText,HtmlText.formatSizeColor("<b>" + city.cityName + "</b>",0x000000,18));
			cityText.width = cityText.textWidth + 5;
			
			checkDownload();
		}
		
		private function checkDownload():void
		{
			var obj:Object = store.readInfo("downloadInfo");
			if(obj){
				for(var i:int = 0;i  < obj.downloadList.length;i++){
					if(obj.downloadList[i] == city.fileName){
						cityIsDone = true;
						
						changeBtnState();
						return;
					}
				}
			}else{
				cityIsDone = false;
			}
		}
		
		private function changeBtnState():void
		{	
			downloadBtn.mouseChildren = false;
			downloadBtn.filters = [ClientConstants.GLOBAL_FILTER_GREY];
			
			TextFieldUtils.setHtmlText(cityText,HtmlText.formatSizeColor("<b>" + city.cityName + "(资源已下载)" + "</b>",0x000000,18));
			cityText.width = cityText.textWidth + 5;
			
			line.changeData(1);
		}
		
		private function showDetail():void
		{
			if(_onShowDetail is Function){
				_onShowDetail(city.id,cityIsDone);
			}
		}
		
		public function downloadThis():void
		{
			loadZip = new SaveZipAsFile();
			
			var url:String = _uri.getCityZip(city.fileName + "/" + city.fileName);
			loadZip.loadProgress = changeProgressLine;
			loadZip.loadSuccess = loadSuccess;
			loadZip.unzip_init(url,1);
		}
		
		private function changeProgressLine(current:Number,total:Number):void
		{
			line.changeData(current / total);
		}
		
		private function loadSuccess():void
		{
			Log.trace("下载完了,正在解压");
			
			var obj:Object = store.readInfo("downloadInfo");
			if(obj != null){
				obj.downloadList.push(city.fileName);
			}else{
				obj = new Object;
				obj = {downloadList:[city.fileName]};
				store.storeInfo("downloadInfo",obj);
			}
			
			this.cityIsDone = true;
			changeBtnState();
		}
		
		public function set onShowDetail(func:Function):void
		{
			_onShowDetail = func;
		}
		
		public function set onChooseCity(func:Function):void
		{
			chooseThisCity = func;
		}
		
		public override function get height():Number
		{
			return backContainer.height;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				downloadBtn.clearThis();
				downloadBtn = null;
				
				detailBtn.clearThis();
				detailBtn = null;
				
				if(loadZip){
					loadZip.clear();
					loadZip = null;
				}
				
				line.clear();
				line = null;
				
				backContainer.removeEventListener(MouseEvent.CLICK,chooseThisHandler);
			}
		}
	}
}